package com.mall.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.api.SmsHomeBrandApi;
import com.mall.pageResult.PageResult;
import com.mall.pojo.SmsHomeBrand;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class HomeBrandService {

    @DubboReference
    private SmsHomeBrandApi homeBrandApi;

    /**
     * 分页查询推荐品牌
     * @param pageNum
     * @param pageSize
     * @param brandName
     * @param recommendStatus
     * @return
     */
    public PageResult<SmsHomeBrand> list(Integer pageNum, Integer pageSize, String brandName, Integer recommendStatus) {
        //查询推荐品牌分页信息
        Page<SmsHomeBrand> page = homeBrandApi.list(pageNum,pageSize,brandName,recommendStatus);
        //组装返回结果数据
        PageResult<SmsHomeBrand> result = new PageResult<>();
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setList(page.getRecords());
        result.setTotal(Convert.toInt(page.getTotal()));
        result.setTotalPage(Convert.toInt(page.getPages()));
        return result;
    }

    /**
     * 批量修改推荐品牌状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    public Integer updateRecommendStatus(Long[] ids, Integer recommendStatus) {
        //根据一组id查询品牌信息
        List<SmsHomeBrand> brandList = homeBrandApi.findBrandByIds(ids);
        //修改这组品牌状态
        brandList = brandList.stream().map(brand->{
            brand.setRecommendStatus(recommendStatus);
            return brand;
        }).collect(Collectors.toList());
        //批量修改推荐品牌状态
        Integer count = 0;
        for (SmsHomeBrand brand : brandList) {
            homeBrandApi.updateBrand(brand);
            count++;
        }

        return count;
    }

    /**
     * 添加首页推荐品牌
     * @param homeBrandList
     * @return
     */
    public Integer create(List<SmsHomeBrand> homeBrandList) {
        Integer count = 0;
        //判断品牌是否已经在推荐列表
        for (SmsHomeBrand brand : homeBrandList) {
            Long brandId = brand.getBrandId();
            //根据品牌id查询推荐品牌信息
            SmsHomeBrand smsHomeBrand = homeBrandApi.findBrandByBrandId(brandId);
            if (ObjectUtil.isNull(smsHomeBrand)){
                //添加推荐品牌
                //设置默认排序和推荐状态
                brand.setSort(0);
                brand.setRecommendStatus(1);
                homeBrandApi.create(brand);
                count++;
            }
        }
        return count;
    }

    /**
     * 修改推荐品牌排序
     * @param id
     * @param sort
     * @return
     */
    public Integer updateSort(Integer id, Integer sort) {
        //sort为null给个默认值0
        if (ObjectUtil.isNull(sort)){
            sort = 0;
        }
        //根据id查询推荐品牌信息
        SmsHomeBrand brand = homeBrandApi.findBrandById(id);
        brand.setSort(sort);
        Integer count = homeBrandApi.updateBrand(brand);
        return count;
    }

    /**
     * 批量删除推荐品牌
     * @param ids
     * @return
     */
    public Integer delete(Long[] ids) {
        Integer count = homeBrandApi.deleteByIds(ids);
        return count;
    }
}
